;;;; Created on 2011-04-27 21:09:38

;6.6
(defun last-element (lst)
  (first (last lst))
  )

(defun last-element2 (lst)
  (first (reverse lst))
  )

;6.7
(defun next-to-last (lst)
  (second (reverse lst))
  )

(defun next-to-last2 (lst)
  (nth (- (length lst) 2) lst)
  )

;6.8
(defun my-butlast (lst)
  (reverse (rest (reverse lst)))
  )

;6.10
(defun PALINDROMEP (lst)
  (if (or (null lst) (= (length lst) 1))
      T
      (and (equal (first lst) (first (last lst))) (PALINDROMEP (reverse (rest (reverse (rest lst))))))
      )
  )

(defun PALINDROMEP2 (lst)
  (equal lst (reverse lst))
  )

;6.11
(defun PALINDROME (lst)
  (append lst (reverse lst))
  )

;6.24
(defun SET-EQUAL (s1 s2)
  (and (subsetp s1 s2) (subsetp s2 s1))
  )

;6.26
;a
(defun RIGHT-SIDE (lst)
  (rest (member '-vs- lst))
  )

;b
(defun LEFT-SIDE (lst)
  (right-side (reverse lst))
  )

;c
(defun COUNT-COMMON (lst1 lst2)
  (length (intersection lst1 lst2))
  )

;d
(defun COMPARE (lst)
  (list (count-common (left-side lst) (right-side lst)) ' common 'features)
  )

;6.35
;a
(setf NERD-STATES '(
(Sleeping . Eating)
(Eating . Waiting-for-a-Computer)
(Waiting-for-a-Computer . Programming)
(Programming . Debugging)
(Debugging . Sleeping)
))

;b
(defun NERDUS (name)
  (rest (ASSOC name NERD-STATES))
  )

;d
(defun SLEEPLESS-NERD (name)
  (let ((result (NERDUS name)))
    (if (equal result 'SLEEPING) (NERDUS result) result)
  )
  )

;e
(defun NERD-ON-CAFFEINE (name)
  (NERDUS (NERDUS name))
  )

;6.42
(defun royal-we (lst)
  (subst 'we 'i lst)
  )